//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Data.Configuration.Design;

namespace EntLibContrib.Resource.Configuration.Design
{
	/// <summary>
	/// Represents a command for adding a database resource provider.
	/// </summary>
	public class AddDataResourceProviderCommand : AddChildNodeCommand
	{
		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="AddDataResourceProviderCommand"/> class with an <see cref="IServiceProvider"/>.
		/// </summary>
		/// <param name="serviceProvider">The mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param>
		public AddDataResourceProviderCommand(IServiceProvider serviceProvider)
			: base(serviceProvider, typeof(DataResourceProviderNode))
		{
		}
		#endregion

		#region Protected Methods
		/// <summary>
		/// Adds the <see cref="DataResourceProviderNode"/> to the current <see cref="ResourceManagerNode"/>. If the Data Application Block configuration is not added to the current application, it is also added.
		/// </summary>
		/// <param name="e">An <see cref="EventArgs"/> containing the event data.</param>
		protected override void OnExecuted(EventArgs e)
		{
			base.OnExecuted(e);

			if (null == CurrentHierarchy.FindNodeByType(typeof(DatabaseSectionNode)))
			{				
				new AddDatabaseSectionNodeCommand(ServiceProvider).Execute(CurrentHierarchy.RootNode);
			}
		}
		#endregion
	}
}
